package com.gitee.candy.other;

/**
 * @author suyl
 * @version V1.0
 * 1.接口返回实例
 * @Description: 接口返回实体
 */
public class ResultBean<T extends Object> {
    /**
     * 失败
     */
    public static final int CODE_FAIL = 100;
    public static final int CODE_SUCCESS = 200;
    public static final int CODE_EXCEPTION = 500;
    /**
     * 无参数
     */
    public static final int CODE_NO_PARAM = 400;
    /**
     * 无效参数
     */
    public static final int CODE_NO_PARAM_VERIFY = 402;
    public static final int CODE_SIGN_ERROR = 401;
    public static final int CODE_NO_AUTH = 403;
    //错误码
    private int code = CODE_SUCCESS;
    //消息
    private String msg;
    //数据
    private T data;
    //令牌
    private String token;

    public static ResultBean success() {
        return new ResultBean(true);
    }

    public static ResultBean fail() {
        return new ResultBean(false);
    }

    public static ResultBean build(int code) {
        return new ResultBean(code);
    }

    public static ResultBean build(int code, String msg) {
        return new ResultBean(code, msg);
    }

    public static ResultBean build(int code, String msg, Object data) {
        return new ResultBean(code, msg, data);
    }

    public ResultBean() {
    }

    public ResultBean(boolean isSuccess) {
        if (isSuccess) {
            this.code = 200;
            this.msg = "查询成功";
        } else {
            this.code = 100;
            this.msg = "查询失败";
        }
    }

    public ResultBean(int code) {
        this.code = code;
    }

    public ResultBean(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 注意：泛型的 T不能为string ，否则就是同 ResultBean(int code,String msg)
     *
     * @param code
     * @param data
     */
    public ResultBean(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResultBean(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}
